<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document Title</title>
    <style>
        #app {
            width: 800px;
            margin: 0px auto;
        }

        table {
            width: 780px;
            border: 1px solid #ccc;
            border-collapse: collapse;
        }

        th, td {
            text-align: center;
            border: 1px solid #ccc;
        }

    </style>
</head>

<body>
<div id="app">
    <table>
        <tr>
            <th>编号</th>
            <th>名称</th>
            <th>价格</th>
            <th>产地</th>
        </tr>
        <tr>
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.price}}</td>
            <td>{{item.city}}</td>
        </tr>
    </table>
    <br>
    <div>
        <input type="button" value="查询商品" @click="queryItemById()"/>
        <input type="button" value="添加商品" @click="addItem()"/>
        <input type="button" value="修改商品" @click="updateItem()"/>
        <input type="button" value="删除商品" @click="removeItem()"/>

    </div>
</div>
<!--使用CDN方式引入vue-->
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.7.4/vue.min.js"></script>
<!--引入axios资源-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.27.2/axios.min.js"></script>


<script>
    new Vue({
        el: '#app',
        data: {
            item: {},  //要查询的商品资料
            newItem: {
                name: '康师傅红烧牛肉面' + new Date().toLocaleString(),
                price: 4,
                city: '杭州'
            }  //要添加的商品资料
        },
        methods: {
            queryItemById: function () {
                //查询商品编号为7的商品资料
                axios.get('http://www.simoniu.com:8080/examdemo/items/7')
                    .then(response => {
                        console.log(response)
                        this.item = response.data.data;
                        console.log(this.item);
                    })
                    .catch(error => {
                        console.log(error)
                    })
            },
            addItem: function () {
                //添加新商品
                axios.post('http://www.simoniu.com:8080/examdemo/items/', this.newItem, {
                    params: {
                        timeout: '1000'
                    }
                }).then(response => {
                    console.log(response)
                }).catch(error => {
                    console.log(error)
                })
            },
            updateItem: function () {
                this.queryItemById(); //查询出商品资料
                this.item.name = '康师傅红烧牛肉面' + new Date().toLocaleString();
                axios.put('http://www.simoniu.com:8080/examdemo/items/' + this.item.id, this.item)
                    .then(response => {
                        console.log(response)
                    })
                    .catch(error => {
                        console.log(error)
                    })
            },
            removeItem: function () {
                //删除编号为100的商品
                axios.delete('http://www.simoniu.com:8080/examdemo/items/100')
                    .then(response => {
                        console.log(response)
                    })
                    .catch(error => {
                        console.log(error)
                    })
            }
        }
    })
</script>
</body>
</html>

